package org.alwayssuper.protocol.commons.transform.realtimev2025;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 2025版动力蓄电池最小并联单元电压数据 (0x07)
 * 参考GB/T 32960.3-2025标准表17
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class BatteryPackVoltageData {

    // 动力蓄电池包个数:1~253个动力蓄电池包个数(由batteryPackVoltageInfos的totalUnit获取)
    private int batteryPackCount;
    
    @Field(totalUnit = 1, desc = "动力蓄电池最小并联单元电压信息列表")
    private List<BatteryPackVoltageInfo> batteryPackVoltageInfos;
    
    @ToString
    @Data
    @Accessors(chain = true)
    public static class BatteryPackVoltageInfo {
        @Field(length = 1, desc = "动力蓄电池包号:有效值范围1~50,0xFE表示异常,0xFF表示无效")
        private int batteryPackId;
        
        @Field(length = 2, desc = "动力蓄电池包电压:有效值范围0~60000(表示0V~6000V),精度0.1V,0xFFFE表示异常,0xFFFF表示无效")
        private int batteryPackVoltage;
        
        @Field(length = 2, desc = "动力蓄电池包电流:有效值范围0~60000(偏移量3000A,表示-3000A~+3000A),精度0.1A,0xFFFE表示异常,0xFFFF表示无效")
        private int batteryPackCurrent;
        // 最小并联单元总数:N 个最小并联单元,有效值范围:1~65531。"0xFF,0xFE"表示异常;"0xFF,0xFF"表示无效(由parallelUnitVoltages的totalUnit获取)
        private int parallelUnitCount;
        
        @Field(totalUnit = 2, desc = "本帧最小并联单元电压:有效值范围0~60000(表示0V~60.000V),精度0.001V")
        private List<ParallelUnitVoltage> parallelUnitVoltages;
        
        // ==================== 传输值与真实值转换方法 ====================
        
        /**
         * 获取动力蓄电池包电压 (V)
         * @return 电压，精度0.1V，异常返回-1，无效返回-2
         */
        public double getBatteryPackVoltage() {
            if (batteryPackVoltage == 0xFFFE) return -1.0; // 异常
            if (batteryPackVoltage == 0xFFFF) return -2.0; // 无效
            return batteryPackVoltage * 0.1; // 传输值×0.1 = 值(V)
        }
        
        /**
         * 设置动力蓄电池包电压 (V)
         * @param voltage 电压值，范围0~6000V
         */
        public void setBatteryPackVoltage(double voltage) {
            if (voltage < 0 || voltage > 6000.0) {
                this.batteryPackVoltage = 0xFFFF; // 无效值
            } else {
                this.batteryPackVoltage = (int) Math.round(voltage * 10); // 值×10 = 传输值
            }
        }
        
        /**
         * 获取动力蓄电池包电流 (A)
         * @return 电流，精度0.1A，异常返回-9999，无效返回-10000
         */
        public double getBatteryPackCurrent() {
            if (batteryPackCurrent == 0xFFFE) return -9999.0; // 异常
            if (batteryPackCurrent == 0xFFFF) return -10000.0; // 无效
            return (batteryPackCurrent - 30000) * 0.1; // (传输值-30000)×0.1 = 值(A)
        }
        
        /**
         * 设置动力蓄电池包电流 (A)
         * @param current 电流值，范围-3000A~+3000A
         */
        public void setBatteryPackCurrent(double current) {
            if (current < -3000.0 || current > 3000.0) {
                this.batteryPackCurrent = 0xFFFF; // 无效值
            } else {
                this.batteryPackCurrent = (int) Math.round(current * 10 + 30000); // (值×10)+30000 = 传输值
            }
        }
        
        /**
         * 获取最小并联单元总数
         * @return 单元总数，通过parallelUnitVoltages列表大小计算
         */
        public int getParallelUnitCount() {
            return parallelUnitVoltages == null ? 0 : parallelUnitVoltages.size();
        }
        
        /**
         * 检查电池包电压是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isBatteryPackVoltageValid() {
            return batteryPackVoltage != 0xFFFE && batteryPackVoltage != 0xFFFF;
        }
        
        /**
         * 检查电池包电流是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isBatteryPackCurrentValid() {
            return batteryPackCurrent != 0xFFFE && batteryPackCurrent != 0xFFFF;
        }
    }
    
    @ToString
    @Data
    @Accessors(chain = true)
    public static class ParallelUnitVoltage {
        @Field(length = 2, desc = "最小并联单元电压:有效值范围0~60000(表示0V~60.000V),精度0.001V,0xFFFE表示异常,0xFFFF表示无效")
        private int parallelUnitVoltage;
        
        // ==================== 传输值与真实值转换方法 ====================
        
        /**
         * 获取最小并联单元电压 (V)
         * @return 电压，精度0.001V，异常返回-1，无效返回-2
         */
        public double getParallelUnitVoltage() {
            if (parallelUnitVoltage == 0xFFFE) return -1.0; // 异常
            if (parallelUnitVoltage == 0xFFFF) return -2.0; // 无效
            return parallelUnitVoltage * 0.001; // 传输值×0.001 = 值(V)，精度提升至0.001V (2025版特性)
        }
        
        /**
         * 设置最小并联单元电压 (V)
         * @param voltage 电压值，范围0~60.000V
         */
        public void setParallelUnitVoltage(double voltage) {
            if (voltage < 0 || voltage > 60.0) {
                this.parallelUnitVoltage = 0xFFFF; // 无效值
            } else {
                this.parallelUnitVoltage = (int) Math.round(voltage * 1000); // 值×1000 = 传输值
            }
        }
        
        /**
         * 检查最小并联单元电压是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isParallelUnitVoltageValid() {
            return parallelUnitVoltage != 0xFFFE && parallelUnitVoltage != 0xFFFF;
        }
    }
    
    // 获取电池包个数
    public int getBatteryPackCount() {
        return batteryPackVoltageInfos == null ? 0 : batteryPackVoltageInfos.size();
    }
}